package org.maxkey.portal.service.impl;
import org.maxkey.portal.core.cam.ResultData;
import org.maxkey.portal.model.Navigation;
import org.maxkey.portal.repository.NavigationRepository;
import org.maxkey.portal.service.NavigationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class NavigationServiceImpl implements NavigationService {

    @Autowired
    NavigationRepository navigationRepository;

    @Override
    public ResultData findById(Long id) {
        try {
            if(id==null){
                return ResultData.error("参数错误");
            }
            Optional<Navigation> u= navigationRepository.findById(id.intValue());
            if(!u.isEmpty()){
                return ResultData.ok(u);
            }
            return ResultData.error("查询不存在");
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }



    /*

        @Override
        public UserInfo findUserByLoginname(String Loginname) {
            return userInfoRepository.findUserByLoginname(Loginname);
        }
    */
    @Override
    public ResultData findAll() {
        try {
            return ResultData.ok(navigationRepository.findAll());
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }

    @Override
    public ResultData deleteById(Long id) {
        try {
            if(id==null){
                return ResultData.error("参数错误");
            }
            navigationRepository.deleteById(id.intValue());
            return ResultData.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }

    @Override
    public ResultData save(Navigation userInfo) {
        try {
            navigationRepository.save(userInfo);
            return ResultData.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }
}
